home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Applications / Muddweller 1.2 / source code / Main / MUDDoc.cp < prev    next >
Encoding:
Text File  |  1994-02-18  |  47.5 KB  |  1,925 lines  |  [TEXT/MPS ]

  1. /* MUDDoc - Implementation of TMUDDoc                                         */
  2.  
  3. #include "MUDDoc.h"
  4.  
  5.  
  6.         // • Toolbox
  7. #ifndef NUM_ALT_ADDRS
  8. #include "AddressXlation.h"
  9. #endif
  10.  
  11. #ifndef __CMINTF__
  12. #include "CMIntf.h"
  13. #endif
  14.  
  15. #ifndef __CRMINTF__
  16. #include "CRMIntf.h"
  17. #endif
  18.  
  19. #ifndef __ERRORS__
  20. #include "Errors.h"
  21. #endif
  22.  
  23. #ifndef __FONTS__
  24. #include "Fonts.h"
  25. #endif
  26.  
  27. #ifndef __OSEVENTS__
  28. #include "OSEvents.h"
  29. #endif
  30.  
  31. #ifndef __RESOURCES__
  32. #include "Resources.h"
  33. #endif
  34.  
  35. #ifndef __STRINGS__
  36. #include "Strings.h"
  37. #endif
  38.  
  39. #ifndef __TOOLUTILS__
  40. #include "ToolUtils.h"
  41. #endif
  42.  
  43.  
  44.         // • MacApp
  45. #ifndef __UMacApp__
  46. #include "UMacApp.h"
  47. #endif
  48.  
  49. #ifndef __UMenuSetup__
  50. #include "UMenuSetup.h"
  51. #endif
  52.  
  53.  
  54.         // • Implementation use
  55. #ifndef __DOCDIALOGS__
  56. #include "DocDialogs.h"
  57. #endif
  58.  
  59. #ifndef __EDSTUFF__
  60. #include "EDStuff.h"
  61. #endif
  62.  
  63. #ifndef __FILTERS__
  64. #include "Filters.h"
  65. #endif
  66.  
  67. #ifndef __GLOBALS__
  68. #include "Globals.h"
  69. #endif
  70.  
  71. #ifndef __MTPSTUFF__
  72. #include "MTPStuff.h"
  73. #endif
  74.  
  75. #ifndef __NOTRACE__
  76. #include "NoTrace.h"
  77. #endif
  78.  
  79. #ifndef __USizerView__
  80. #include "USizerView.h"
  81. #endif
  82.  
  83.  
  84. //------------------------------------------------------------------------------
  85.  
  86. struct Header {
  87.     unsigned long creator;
  88.     long version;
  89. };
  90.  
  91. struct DocInfo1 {
  92.     Boolean doConnect;
  93.     unsigned char endLine;
  94.     Boolean skip;
  95.     Boolean echo;
  96.     Boolean update;
  97.     char filler1;
  98.     char rtab;
  99.     char stab;
  100.     unsigned char intChar;
  101.     char useCTB;
  102.     long tcpAddr;
  103.     short tcpPort;
  104.     short mtpPort;
  105.     short logSize;
  106.     short histSize;
  107.     long mtpDirID;
  108.     VPoint wLoc;
  109.     VPoint wSize;
  110.     Size configSize;
  111. };
  112.  
  113. struct DocInfo2 {
  114.     Boolean useCTB, doConnect, skip, echo;
  115.     Boolean update, useTelnet, parseVT100, cvtTab;
  116.     unsigned char endLine, intChar, protocol, filler1;
  117.     short rtab, stab, fontSize, tileHeight;
  118.     short tcpPort, mtpPort;
  119.     short logSize, histSize;
  120.     long mtpDirID;
  121.     unsigned long textSig;
  122.     VPoint wLoc, wSize;
  123.     Size configSize;
  124. };
  125.  
  126. struct DocInfo3 {
  127.     Boolean autoScroll, filler1;
  128.     short maxCmdSize;
  129.     short numMacros;
  130. };
  131.  
  132. const Header currentHeader = {kSignature, 3};
  133. const int kCMBufferSize = 4 * 1024;
  134. const int kTCPBufferSize = 16 * 1024;
  135. const Rect kMaxChooseSize = {0, 0, 308, 498};
  136. const int kTmpBufSize = 512;
  137. const unsigned long kTextSig = 'MPS ';
  138. const unsigned char *kCreatorApp = "\pMPW Shell";
  139. const unsigned char *kHostName = "\p0.0.0.0";
  140.  
  141. //------------------------------------------------------------------------------
  142.  
  143. #pragma segment MACommandRes
  144.  
  145. class TMUDCmd: public TCommand {
  146. public:
  147.     virtual pascal void DoIt (void);
  148.     virtual pascal void IMUDCmd (TMUDDoc *itsDocument, CmdNumber itsCmdNumber);
  149. };
  150.  
  151.  
  152. pascal void TMUDCmd::DoIt (void)
  153. {
  154.     switch (fCmdNumber) {
  155.     case cConfigure:
  156.         if (((TMUDDoc *) fChangedDocument)->fUseCTB)
  157.             ((TMUDDoc *) fChangedDocument)->Configure ();
  158.         else
  159.             ConfigTCP ((TMUDDoc *) fChangedDocument);
  160.         break;
  161.     case cUseCTB:
  162.         ((TMUDDoc *) fChangedDocument)->UseCTB ();
  163.         break;
  164.     case cPrefs:
  165.         Preferences ((TMUDDoc *) fChangedDocument);
  166.         break;
  167.     case cComm:
  168.         Communication ((TMUDDoc *) fChangedDocument);
  169.         break;
  170.     case cLogFile:
  171.         if (((TMUDDoc *) fChangedDocument)->fLogFilter->fLogging)
  172.             ((TMUDDoc *) fChangedDocument)->fLogFilter->CloseLog ();
  173.         else
  174.             ((TMUDDoc *) fChangedDocument)->fLogFilter->OpenLog ();
  175.         break;
  176.     case cConnect:
  177.         if (((TMUDDoc *) fChangedDocument)->fConnected)
  178.             ((TMUDDoc *) fChangedDocument)->Disconnect ();
  179.         else
  180.             ((TMUDDoc *) fChangedDocument)->Connect ();
  181.         break;
  182.     case cUpdate:
  183.         ((TMUDDoc *) fChangedDocument)->Update ();
  184.         break;
  185.     case cUpdateAll:
  186.         ((TMUDDoc *) fChangedDocument)->UpdateAll ();
  187.         break;
  188.     case cUpload:
  189.         ((TMUDDoc *) fChangedDocument)->Upload ();
  190.         break;
  191.     case cDownload:
  192.         ((TMUDDoc *) fChangedDocument)->Download ();
  193.         break;
  194.     case cSetup:
  195.         Setup ((TMUDDoc *) fChangedDocument);
  196.         break;
  197.     case cMacros:
  198.         EditMacros (fChangedDocument);
  199.         break;
  200.     case cBreak:
  201.         ((TMUDDoc *) fChangedDocument)->Break ();
  202.         break;
  203.     case cInterrupt:
  204.         ((TMUDDoc *) fChangedDocument)->Interrupt ();
  205.         break;
  206.     case cSendFile:
  207.         ((TMUDDoc *) fChangedDocument)->SendFile ();
  208.         break;
  209.     }
  210. }
  211.  
  212.  
  213. pascal void TMUDCmd::IMUDCmd (TMUDDoc *itsDocument, CmdNumber itsCmdNumber)
  214. {
  215.     ICommand (itsCmdNumber, itsDocument, NULL, NULL);
  216.     fCanUndo = FALSE;
  217.     fCausesChange = FALSE;
  218. }
  219.  
  220. //------------------------------------------------------------------------------
  221.  
  222. #pragma segment SMUDDocRes
  223.  
  224. class TMUDIdler: public TEvtHandler {
  225. public:
  226.     TMUDDoc *fDocument;
  227.     
  228.     virtual pascal Boolean DoIdle (IdlePhase phase);
  229.     virtual pascal void IMUDIdler (TMUDDoc *itsDocument);
  230. };
  231.  
  232.  
  233. pascal Boolean TMUDIdler::DoIdle (IdlePhase phase)
  234. {
  235.     fDocument->DoIdle (phase);
  236.     return FALSE;
  237. }
  238.  
  239.  
  240. pascal void TMUDIdler::IMUDIdler (TMUDDoc *itsDocument)
  241. {
  242.     IEvtHandler (NULL);
  243.     fIdleFreq = 0;
  244.     fDocument = itsDocument;
  245. }
  246.  
  247. //------------------------------------------------------------------------------
  248.  
  249. #pragma segment SFileTrans
  250.  
  251. class TMUDUpdater: public TWindow {
  252. public:
  253.     virtual pascal void DoChoice (TView *origView, short itsChoice);
  254. };
  255.  
  256.  
  257. pascal void TMUDUpdater::DoChoice (TView *, short itsChoice)
  258. {
  259.     if (itsChoice == mButtonHit) gStop = TRUE;
  260. }
  261.  
  262.  
  263. //------------------------------------------------------------------------------
  264.  
  265. #pragma segment SMUDDocRes
  266.  
  267. pascal void ASRDummy (StreamPtr , unsigned short , Ptr , unsigned short ,
  268.         struct ICMPReport *)
  269. {
  270. }
  271.  
  272. //------------------------------------------------------------------------------
  273.  
  274. #pragma segment SMUDDocRes
  275.  
  276. pascal void TMUDDoc::Break (void)
  277. {
  278.     if (fUseCTB && fConnected) CMBreak (fConn, 2, FALSE, NULL);
  279. }
  280.  
  281. //------------------------------------------------------------------------------
  282.  
  283. #pragma segment SMUDDocRes
  284.  
  285. pascal void TMUDDoc::CheckConnection (void)
  286. {
  287.     CMBufferSizes sizes;
  288.     CMStatFlags status;
  289.     TCPiopb pb;
  290.     OSErr error;
  291.     int state;
  292.  
  293.     if (fConnected) {
  294.         if (fUseCTB) {
  295.             fConnected = (fConn != NULL) &&
  296.                 (CMStatus (fConn, sizes, &status) == noErr) &&
  297.                 ((status & (cmStatusOpen + cmStatusOpening)) != 0);
  298.         } else {
  299.             pb.ioNamePtr = NULL;
  300.             pb.ioCRefNum = gTCPRef;
  301.             pb.tcpStream = fTCPStream;
  302.             pb.csCode = TCPStatus;
  303.             pb.csParam.status.userDataPtr = (Ptr) fBlock;
  304.             PBControl ((ParamBlockRec *) &pb, FALSE);
  305.             state = (pb.csParam.status.connectionState & -2);
  306.             fConnected = (pb.ioResult == noErr) && (state != 0) &&
  307.                 (state != 2) && (state != 20) && (state != 14);
  308.             if (!fConnected && (pb.ioResult == noErr) && (state != 0)) {
  309.                 pb.ioNamePtr = NULL;
  310.                 pb.ioCRefNum = gTCPRef;
  311.                 pb.tcpStream = fTCPStream;
  312.                 pb.csCode = TCPAbort;
  313.                 PBControl ((ParamBlockRec *) &pb, FALSE);
  314.             }
  315.         }
  316.         if (!fConnected) {
  317.             fBlock->wtBufIn = 0;
  318.             fBlock->wtBufOut = 0;
  319.             fBlock->wtBufChars = 0;
  320.             InvalidateMenuBar ();
  321.             gApplication->SetupTheMenus ();
  322.         }
  323.         if (fBlock->ioError != noErr) {
  324.             error = fBlock->ioError;
  325.             fBlock->ioError = noErr;
  326.             gApplication->ShowError (error, 0);
  327.         }
  328.     }
  329. }
  330.  
  331. //------------------------------------------------------------------------------
  332.  
  333. #pragma segment MAClose
  334.  
  335. pascal void TMUDDoc::Close (void)
  336. {
  337.     TLogoutMacro *logout;
  338.     
  339.     if (fClosing) return;
  340.     if (fConnected) {
  341.         logout = new TLogoutMacro;
  342.         FailNIL (logout);
  343.         logout->IMacro (this, fLogout);
  344.         logout->fDoneCmd = cClose;
  345.         return;
  346.     }
  347.     if (fLogFilter->fLogging) fLogFilter->CloseLog ();
  348.     InvalidateMenuBar ();
  349.     gIdlePhase = idleBegin;
  350.     inherited::Close();
  351. }
  352.  
  353. //------------------------------------------------------------------------------
  354.  
  355. #pragma segment MAClose
  356.  
  357. pascal void TMUDDoc::CloseConnection (void)
  358. {
  359.     TCPiopb pb;
  360.  
  361.     Disconnect ();
  362.     if (fUseCTB) {
  363.         CMActivate (fConn, FALSE);
  364.         if (fConn) {
  365.             HUnlock ((Handle) fConn);
  366.             CMDispose (fConn);
  367.             fConn = NULL;
  368.         }
  369.     } else {
  370.         pb.ioNamePtr = NULL;
  371.         pb.ioCRefNum = gTCPRef;
  372.         pb.tcpStream = fTCPStream;
  373.         pb.csCode = TCPRelease;
  374.         PBControl ((ParamBlockRec *) &pb, FALSE);
  375.     }
  376.     if (fBuffer) {
  377.         DisposIfPtr (fBuffer);
  378.         fBuffer = NULL;
  379.     }
  380.     if (fIdler) {
  381.         gApplication->InstallCohandler (fIdler, FALSE);
  382.         DisposIfHandle (fIdler);
  383.         fIdler = NULL;
  384.     }
  385.     fDoConnect = FALSE;
  386. }
  387.  
  388. //------------------------------------------------------------------------------
  389.  
  390. #pragma segment SMUDDocRes
  391.  
  392. pascal void TMUDDoc::Configure ()
  393. {
  394.     short result;
  395.     ConnHandle tmp;
  396.     Str255 name;
  397.     Rect r;
  398.     Point pt;
  399.     Boolean wasPermanent;
  400.     
  401.     r = kMaxChooseSize;
  402.     CenterRectOnScreen (&r, TRUE, TRUE, TRUE);
  403.     pt.v = r.top;
  404.     pt.h = r.left;
  405.     CMActivate (fConn, FALSE);
  406.     wasPermanent = PermAllocation (TRUE);
  407.     HUnlock ((Handle) fConn);
  408.     tmp = fConn;
  409.     result = CMChoose (&tmp, pt, NULL);
  410.     fConn = tmp;
  411.     MoveHHi ((Handle) fConn);
  412.     HLock ((Handle) fConn);
  413.     wasPermanent = PermAllocation (wasPermanent);
  414.     CMActivate (fConn, TRUE);
  415.     switch (result) {
  416.     case chooseDisaster:
  417.     case chooseFailed:
  418.         Failure (0, msgOpenFailed);
  419.         break;
  420.     case chooseOKMinor:
  421.         ++fChangeCount;
  422.         break;
  423.     case chooseOKMajor:
  424.         ++fChangeCount;
  425.         CMGetToolName ((*fConn)->procID, name);
  426.         CopyStr255 (name, fToolName);
  427.         break;
  428.     }
  429. }
  430.  
  431. //------------------------------------------------------------------------------
  432.  
  433. #pragma segment SMUDDocRes
  434.  
  435. pascal void ResultProc (struct hostInfo *, char *)
  436. {
  437. }
  438.  
  439. pascal void ResolverHandler (short , long , void * )
  440. {
  441.     CloseResolver ();
  442. }
  443.  
  444. pascal void TMUDDoc::Connect (void)
  445. {
  446.     TCPiopb *pbp;
  447.     FailInfo fi;
  448.     OSErr err;
  449.     Str255 tmp;
  450.     hostInfo info;
  451.     TLoginMacro *login;
  452.  
  453.     if (!fConnected) {
  454.         fBlock->rdBufIn = 0;
  455.         fBlock->rdBufOut = 0;
  456.         fBlock->rdBufChars = 0;
  457.         fBlock->wtBufIn = 0;
  458.         fBlock->wtBufOut = 0;
  459.         fBlock->wtBufChars = 0;
  460.         fBlock->wtCMChars = 0;
  461.         fBlock->ioError = noErr;
  462.         if (fUseCTB) {
  463.             FailCMErr (CMOpen (fConn, FALSE, NULL, -1));
  464.             CMSetUserData (fConn, (long) fBlock);
  465.             fConnected = TRUE;
  466.             fDoConnect = TRUE;
  467.         } else {
  468.             CopyStr255 (fHostName, tmp);
  469.             P2CStr (tmp);
  470.             FailOSErr (OpenResolver (NULL));
  471.             CatchFailures (&fi, ResolverHandler, kDummyLink);
  472.             err = StrToAddr ((char *) tmp, &info, ResultProc, NULL);
  473.             while (err == cacheFault) err = info.rtnCode;
  474.             FailOSErr (err);
  475.             Success (&fi);
  476.             FailOSErr (CloseResolver ());
  477.             login = new TLoginMacro;
  478.             FailNIL (login);
  479.             login->IMacro (this, fLogin);
  480.             login->fStartTicks = TickCount ();
  481.             fTCPAddr = info.addr [0];
  482.             pbp = &fBlock->pb;
  483.             pbp->ioCompletion = NULL;
  484.             pbp->ioNamePtr = NULL;
  485.             pbp->ioCRefNum = gTCPRef;
  486.             pbp->tcpStream = fTCPStream;
  487.             pbp->csCode = TCPActiveOpen;
  488.             pbp->csParam.open.ulpTimeoutValue = kTCPTimeout;
  489.             pbp->csParam.open.ulpTimeoutAction = 1;
  490.             pbp->csParam.open.validityFlags = timeoutValue + timeoutAction;
  491.             pbp->csParam.open.commandTimeoutValue = kTCPTimeout;
  492.             pbp->csParam.open.remoteHost = fTCPAddr;
  493.             pbp->csParam.open.remotePort = fTCPPort;
  494.             pbp->csParam.open.localPort = 0;
  495.             pbp->csParam.open.tosFlags = 0;
  496.             pbp->csParam.open.precedence = 0;
  497.             pbp->csParam.open.dontFrag = FALSE;
  498.             pbp->csParam.open.timeToLive = 0;
  499.             pbp->csParam.open.security = 0;
  500.             pbp->csParam.open.optionCnt = 0;
  501.             pbp->csParam.open.userDataPtr = (Ptr) fBlock;
  502.             PBControl ((ParamBlockRec *) pbp, TRUE);
  503.         }
  504.     }
  505. }
  506.  
  507. //------------------------------------------------------------------------------
  508.  
  509. #pragma segment MAClose
  510.  
  511. pascal void TMUDDoc::Disconnect (void)
  512. {
  513.     TCPiopb pb;
  514.  
  515.     CheckConnection ();
  516.     if (fConnected) {
  517.         fConnected = FALSE;
  518.         if (fUseCTB) {
  519.             CMIOKill (fConn, cmDataIn);
  520.             CMIOKill (fConn, cmDataOut);
  521.             fBlock->wtCMChars = 0;
  522.             CMClose (fConn, FALSE, NULL, 0, TRUE);
  523.         } else {
  524.             pb.ioNamePtr = NULL;
  525.             pb.ioCRefNum = gTCPRef;
  526.             pb.tcpStream = fTCPStream;
  527.             pb.csCode = TCPAbort;
  528.             PBControl ((ParamBlockRec *) &pb, FALSE);
  529.         }
  530.     }
  531. }
  532.  
  533. //------------------------------------------------------------------------------
  534.  
  535. #pragma segment SMUDDocRes
  536.  
  537. pascal Boolean TMUDDoc::DoCMEvent (ConnHandle hConn, EventInfo *evtInfo)
  538. {
  539.     if (fUseCTB && (fConn != NULL) && (fConn == hConn)) {
  540.         CMEvent (fConn, evtInfo->thePEvent);
  541.         return TRUE;
  542.     } else
  543.         return FALSE;
  544. }
  545.  
  546. //------------------------------------------------------------------------------
  547.  
  548. #pragma segment SMUDDocRes
  549.  
  550. pascal Boolean TMUDDoc::DoCMMenu (short theMenu, short theItem)
  551. {
  552.     if (fUseCTB && (fConn != NULL))
  553.         return CMMenu (fConn, theMenu, theItem);
  554.     else
  555.         return FALSE;
  556. }
  557.  
  558. //------------------------------------------------------------------------------
  559.  
  560. #pragma segment SMUDDocRes
  561.  
  562. pascal void TMUDDoc::DoCMResume (Boolean switchIn)
  563. {
  564.     if (fUseCTB && (fConn != NULL)) CMResume (fConn, switchIn);
  565. }
  566.  
  567. //------------------------------------------------------------------------------
  568.  
  569. #pragma segment SMUDDocRes
  570. // WtCompletor must be in same segment as CMtoOSErr!!!
  571.  
  572. static Boolean gIdleEntered = FALSE;
  573.  
  574. pascal void WtCompletor (ConnHandle hConn)
  575. {
  576.     IOBlock *blockPtr;
  577.     OSErr err;
  578.     
  579.     blockPtr = (IOBlock *) (**hConn).userData;
  580.         // Don't use CMGetUserData unless it is in the same segment!
  581.     blockPtr->wtBufOut += blockPtr->wtCMChars;
  582.     if (blockPtr->wtBufOut >= kRWBufferSize) blockPtr->wtBufOut = 0;
  583.     blockPtr->wtBufChars -= blockPtr->wtCMChars;
  584.     blockPtr->wtCMChars = 0;
  585.     err = CMtoOSErr ((**hConn).errCode);
  586.     if ((err != noErr) && (blockPtr->ioError == noErr))
  587.         blockPtr->ioError = err;
  588. }
  589.  
  590. void IOCompletion (struct TCPiopb *iopb)
  591. {
  592.     IOBlock *blockPtr;
  593.     
  594.     blockPtr = (IOBlock *) iopb->csParam.send.userDataPtr;
  595.     blockPtr->wtBufOut += blockPtr->wtCMChars;
  596.     if (blockPtr->wtBufOut >= kRWBufferSize) blockPtr->wtBufOut = 0;
  597.     blockPtr->wtBufChars -= blockPtr->wtCMChars;
  598.     blockPtr->wtCMChars = 0;
  599.     if ((iopb->ioResult < 0) && (blockPtr->ioError == noErr))
  600.         blockPtr->ioError = iopb->ioResult;
  601. }
  602.  
  603. pascal void IdleEnteredHandler (short , long , void *thisDocument)
  604. {
  605.     long count;
  606.     IOBlock *blockPtr;
  607.     
  608.     gIdleEntered = FALSE;
  609.     blockPtr = ((TMUDDoc *) thisDocument)->fBlock;
  610.     count = blockPtr->rdBufChars;
  611.     if (blockPtr->rdBufOut + count > kRWBufferSize)
  612.         count = kRWBufferSize - blockPtr->rdBufOut;
  613.     blockPtr->rdBufOut += count;
  614.     if (blockPtr->rdBufOut >= kRWBufferSize) blockPtr->rdBufOut = 0;
  615.     blockPtr->rdBufChars -= count;
  616. }
  617.  
  618. pascal Boolean TMUDDoc::DoIdle (IdlePhase phase)
  619. {
  620.     long count;
  621.     CMFlags flags;
  622.     CMStatFlags sflags;
  623.     CMBufferSizes bsizes;
  624.     TCPiopb pb;
  625.     FailInfo fi;
  626.     TMacro *oldmac;
  627.     
  628.     while (fRunList) {
  629.         oldmac = fRunList;
  630.         fRunList->DoIdle ();
  631.         if (oldmac == fRunList) break; // loop only if the macro freed itself
  632.     }
  633.     if ((gEventLevel <= 1) && (phase == idleContinue) && !fRunList &&
  634.             fLogWindow->fIsActive)
  635.         gStop = FALSE;
  636.     if (fUseCTB && (fConn != NULL)) CMIdle (fConn);
  637.     CheckConnection ();
  638.     if (fConnected) {
  639.         if (fBlock->wtBufChars > 0) {
  640.             if (fBlock->wtCMChars == 0) {
  641.                 count = fBlock->wtBufChars;
  642.                 if (fBlock->wtBufOut + count > kRWBufferSize)
  643.                     count = kRWBufferSize - fBlock->wtBufOut;
  644.                 if (fUseCTB) {
  645.                     fBlock->wtCMChars = count;
  646.                     FailCMErr (CMWrite (fConn, (Ptr) (fBlock->wtBuf +
  647.                         fBlock->wtBufOut), &count, cmData, TRUE,
  648.                         (ProcPtr) WtCompletor, -1, 0));
  649.                 } else {
  650.                     fBlock->pb.ioCompletion = IOCompletion;
  651.                     fBlock->pb.ioNamePtr = NULL;
  652.                     fBlock->pb.ioCRefNum = gTCPRef;
  653.                     fBlock->pb.tcpStream = fTCPStream;
  654.                     fBlock->pb.csCode = TCPSend;
  655.                     fBlock->pb.csParam.send.ulpTimeoutValue = kTCPTimeout;
  656.                     fBlock->pb.csParam.send.ulpTimeoutAction = 1;
  657.                     fBlock->pb.csParam.send.validityFlags = timeoutValue +
  658.                         timeoutAction;
  659.                     fBlock->pb.csParam.send.pushFlag = TRUE;
  660.                     fBlock->pb.csParam.send.urgentFlag = FALSE;
  661.                     fBlock->pb.csParam.send.wdsPtr =
  662.                         (Ptr) &(fBlock->wdsLength);
  663.                     fBlock->pb.csParam.send.sendFree = 0;
  664.                     fBlock->pb.csParam.send.sendLength = 0;
  665.                     fBlock->pb.csParam.send.userDataPtr = (Ptr) fBlock;
  666.                     fBlock->wdsLength = (unsigned short) count;
  667.                     fBlock->wdsPtr = (Ptr) (fBlock->wtBuf +
  668.                         fBlock->wtBufOut);
  669.                     fBlock->wdsEndmarker = 0;
  670.                     pb.csParam.send.userDataPtr = (Ptr) fBlock;
  671.                     fBlock->wtCMChars = count;
  672.                     PBControl ((ParamBlockRec *) &fBlock->pb, TRUE);
  673.                     if ((fBlock->pb.ioResult != 1) &&
  674.                             (fBlock->ioError == noErr))
  675.                         FailOSErr (fBlock->pb.ioResult);
  676.                 }
  677.             }
  678.         }
  679.         if (fBlock->rdBufChars < kRWBufferSize) {
  680.             count = kRWBufferSize - fBlock->rdBufChars;
  681.             if (fBlock->rdBufIn + count > kRWBufferSize)
  682.                 count = kRWBufferSize - fBlock->rdBufIn;
  683.             if (fUseCTB) {
  684.                 FailCMErr (CMStatus (fConn, bsizes, &sflags));
  685.                 if (((sflags & cmStatusClosing) == 0) &&
  686.                         (bsizes [cmDataIn] > 0)) {
  687.                     FailCMErr (CMRead (fConn, (Ptr) (fBlock->rdBuf +
  688.                         fBlock->rdBufIn), &count, cmData, FALSE, NULL, 0,
  689.                         &flags));
  690.                 } else
  691.                     count = 0;
  692.             } else {
  693.                 pb.ioNamePtr = NULL;
  694.                 pb.ioCRefNum = gTCPRef;
  695.                 pb.tcpStream = fTCPStream;
  696.                 pb.csCode = TCPStatus;
  697.                 pb.csParam.status.userDataPtr = (Ptr) fBlock;
  698.                 FailOSErr (PBControl ((ParamBlockRec *) &pb, FALSE));
  699.                 if (pb.csParam.status.amtUnreadData < count)
  700.                     count = pb.csParam.status.amtUnreadData;
  701.                 if ((count > 0) && (pb.csParam.status.connectionState == 8)) {
  702.                     pb.ioNamePtr = NULL;
  703.                     pb.ioCRefNum = gTCPRef;
  704.                     pb.tcpStream = fTCPStream;
  705.                     pb.csCode = TCPRcv;
  706.                     pb.csParam.receive.commandTimeoutValue = 1;
  707.                     pb.csParam.receive.rcvBuff = (Ptr) (fBlock->rdBuf +
  708.                         fBlock->rdBufIn);
  709.                     pb.csParam.receive.rcvBuffLen = (unsigned short) count;
  710.                     pb.csParam.receive.rdsPtr = NULL;
  711.                     pb.csParam.receive.rdsLength = 0;
  712.                     pb.csParam.receive.userDataPtr = (Ptr) fBlock;
  713.                     PBControl ((ParamBlockRec *) &pb, FALSE);
  714.                     if (pb.ioResult != noErr) {
  715.                         if (fBlock->ioError == noErr)
  716.                             FailOSErr (pb.ioResult);
  717.                         else
  718.                             count = 0; // CheckConnection will display the error
  719.                     } else
  720.                         count = pb.csParam.receive.rcvBuffLen;
  721.                 }
  722.             }
  723.             if (count > 0) {
  724.                 fBlock->rdBufIn += count;
  725.                 if (fBlock->rdBufIn >= kRWBufferSize) fBlock->rdBufIn = 0;
  726.                 fBlock->rdBufChars += count;
  727.             }
  728.         }
  729.     } else if (fBlock->wtBufChars > 0) {
  730.         fBlock->wtBufChars = 0;
  731.         fBlock->wtBufOut = fBlock->wtBufIn;
  732.     }
  733.     if (!gIdleEntered && (fBlock->rdBufChars > 0)) {
  734.         CatchFailures (&fi, IdleEnteredHandler, (void *) this);
  735.         gIdleEntered = TRUE;
  736.         count = fBlock->rdBufChars;
  737.         if (fBlock->rdBufOut + count > kRWBufferSize)
  738.             count = kRWBufferSize - fBlock->rdBufOut;
  739.         count = fInputFilter->Filter (fBlock->rdBuf + fBlock->rdBufOut, count);
  740.         if (count > 0) {
  741.             fBlock->rdBufOut += count;
  742.             if (fBlock->rdBufOut >= kRWBufferSize) fBlock->rdBufOut = 0;
  743.             fBlock->rdBufChars -= count;
  744.         }
  745.         Success (&fi);
  746.         gIdleEntered = FALSE;
  747.     }
  748.     return FALSE;
  749. }
  750.  
  751. //------------------------------------------------------------------------------
  752.  
  753. #pragma segment AOpen
  754.  
  755. pascal void TMUDDoc::DoInitialState (void)
  756. {
  757.     short fontSize;
  758.     Str255 tmp;
  759.     
  760.     fToolName [0] = 0;
  761.     fUseCTB = !gTCPAvailable;
  762.     fDoConnect = FALSE;
  763.     fSkipCR = TRUE;
  764.     fEcho = TRUE;
  765.     fDoEcho = TRUE;
  766.     fUpdate = TRUE;
  767.     fUseTelnet = TRUE;
  768.     fParseVT100 = TRUE;
  769.     fCvtTab = TRUE;
  770.     fEndLine = 1;
  771.     fInterrupt = 3;
  772.     fProtocol = 0;
  773.     fTabSize = 8;
  774.     fSTab = 4;
  775.     fFontSize = 9;
  776.     fTileHeight = 0;
  777.     fTCPPort = 0;
  778.     fMTPPort = 0;
  779.     fLogSize = kStdLogSize;
  780.     fHistSize = kStdHistSize;
  781.     fTextSig = kTextSig;
  782.     fLoc = gZeroVPt;
  783.     fSize = gZeroVPt;
  784.     MTPDirSetup (this, gDefVRefNum, gDefDirID);
  785.     fPlayer [0] = 0;
  786.     fPasswd [0] = 0;
  787.     fFontNum = monaco;
  788.     fontSize = fFontSize;
  789.     GetPortFontInfo (fFontNum, tmp, &fontSize);
  790.     CopyStr255 (tmp, fFontName);
  791.     GetIndString (tmp, kMUDStrings, kmsHost);
  792.     CopyStr255 (tmp, fHostName);
  793.     GetIndString (tmp, kMUDStrings, kmsCreator);
  794.     CopyStr255 (tmp, fCreatorApp);
  795.     fMaxCmdSize = kMaxCmdSize;
  796.     fAutoScroll = TRUE;
  797.     fLogin = NewMacroRec ();
  798.     fLogout = NewMacroRec ();
  799. }
  800.  
  801. //------------------------------------------------------------------------------
  802.  
  803. #pragma segment AOpen
  804.  
  805. pascal void TMUDDoc::DoMakeViews (Boolean forPrinting)
  806. {
  807.     TWindow *aWindow;
  808.  
  809.     if (forPrinting)
  810.         Failure (0, msgPrintFailed);
  811.     else
  812.     {
  813.         aWindow = NewTemplateWindow (kMUDWindID, this);
  814.         FailNIL (aWindow);
  815.         if ((fSize.h != 0) && (fSize.v != 0)) {
  816.             aWindow->Locate (fLoc.h, fLoc.v, kDontInvalidate);
  817.             aWindow->Resize (fSize.h, fSize.v, kDontInvalidate);
  818.             aWindow->fStaggered = TRUE;
  819.         }
  820.         fLogWindow = (TLogWindow *) aWindow;
  821.         OpenConnection ();
  822.     }
  823. }
  824.  
  825. //------------------------------------------------------------------------------
  826.  
  827. #pragma segment MASelCommand
  828.  
  829. pascal TCommand *TMUDDoc::DoMenuCommand (CmdNumber aCmdNumber)
  830. {
  831.     TMUDCmd *aMUDCmd;
  832.  
  833.     switch (aCmdNumber) {
  834.     case cConfigure:
  835.     case cUseCTB:
  836.     case cPrefs:
  837.     case cComm:
  838.     case cLogFile:
  839.     case cConnect:
  840.     case cUpdate:
  841.     case cUpdateAll:
  842.     case cUpload:
  843.     case cDownload:
  844.     case cSetup:
  845.     case cBreak:
  846.     case cInterrupt:
  847.     case cSendFile:
  848.     case cMacros:
  849.         aMUDCmd = new TMUDCmd;
  850.         FailNIL (aMUDCmd);
  851.         aMUDCmd->IMUDCmd (this, aCmdNumber);
  852.         return aMUDCmd;
  853.     default:
  854.         return inherited::DoMenuCommand (aCmdNumber);
  855.     }
  856. }
  857.  
  858. //------------------------------------------------------------------------------
  859.  
  860. #pragma segment MAReadFile
  861.  
  862. pascal void TMUDDoc::DoNeedDiskSpace (long *dataForkBytes, long *)
  863. {
  864.     MHandle mh;
  865.     
  866.     inherited::DoNeedDiskSpace (dataForkBytes, dataForkBytes);
  867.     *dataForkBytes += sizeof (Header) + fToolName [0] + 1 + sizeof (DocInfo2) +
  868.         fMTPVol [0] + 1 + fPlayer [0] + 1 + fPasswd [0] + 1 + sizeof (long) +
  869.         GetHandleSize ((Handle) fUpdateList);
  870.     if (fUseCTB) {
  871.         if (fConfig) DisposIfPtr (fConfig);
  872.         fConfig = CMGetConfig (fConn);
  873.         if (fConfig != NULL) {
  874.             *dataForkBytes += GetPtrSize (fConfig);
  875.             DisposIfPtr (fConfig);
  876.             fConfig = NULL;
  877.         }
  878.     }
  879.     *dataForkBytes += fFontName[0] + 1 + fHostName[0] + 1 + fCreatorApp[0] + 1;
  880.     *dataForkBytes += sizeof (DocInfo3) + MacroRecSize (fLogin) +
  881.          MacroRecSize (fLogout);
  882.     mh = fMacroList;
  883.     while (mh) {
  884.         *dataForkBytes += MacroRecSize (mh);
  885.         mh = (**mh).mNext;
  886.     }
  887. }
  888.  
  889. //------------------------------------------------------------------------------
  890.  
  891. #pragma segment MAReadFile
  892.  
  893. pascal void DoReadHandler (short , long , void * )
  894. {
  895.     CloseResolver ();
  896. }
  897.  
  898. pascal void TMUDDoc::DoRead (short aRefNum, Boolean rsrcExists,
  899.         Boolean forPrinting)
  900. {
  901.     Header head;
  902.     short fontSize;
  903.     long count, len, configSize;
  904.     Str255 tmp;
  905.     DocInfo1 dInfo1;
  906.     DocInfo2 dInfo2;
  907.     DocInfo3 dInfo3;
  908.     SignedByte oldState;
  909.     EventRecord theEvent;
  910.     FailInfo fi;
  911.     MHandle mh;
  912.     int i;
  913.     
  914.     OSEventAvail (everyEvent, &theEvent);
  915.     count = sizeof (currentHeader);
  916.     FailOSErr (FSRead (aRefNum, &count, (Ptr) &head));
  917.     if ((head.creator != kSignature) || (head.version < 1) ||
  918.             (head.version > 3))
  919.         Failure (errDocFormat, 0);
  920.     if (head.version < 3) {
  921.         CopyStr255 (*fTitle, tmp);
  922.         ParamText (tmp, "\p", "\p", "\p");
  923.         MacAppAlert (phConvertID, NULL);
  924.         ++fChangeCount;
  925.     }
  926.     inherited::DoRead (aRefNum, rsrcExists, forPrinting);
  927.     count = 1;
  928.     FailOSErr (FSRead (aRefNum, &count, (Ptr) tmp));
  929.     count = tmp [0];
  930.     FailOSErr (FSRead (aRefNum, &count, (Ptr) (tmp + 1)));
  931.     CopyStr255 (tmp, fToolName);
  932.     if (head.version == 1) {
  933.         ++fChangeCount;
  934.         count = sizeof (dInfo1);
  935.         FailOSErr (FSRead (aRefNum, &count, (Ptr) &dInfo1));
  936.         fDoConnect = dInfo1.doConnect;
  937.         fEndLine = dInfo1.endLine;
  938.         fSkipCR = dInfo1.skip;
  939.         fEcho = dInfo1.echo;
  940.         fUpdate = dInfo1.update;
  941.         fTabSize = dInfo1.rtab;
  942.         fSTab = dInfo1.stab;
  943.         fInterrupt = dInfo1.intChar;
  944.         fUseCTB = dInfo1.useCTB;
  945.         fTCPAddr = dInfo1.tcpAddr;
  946.         fTCPPort = dInfo1.tcpPort;
  947.         fMTPPort = dInfo1.mtpPort;
  948.         fLogSize = dInfo1.logSize / 12;
  949.         if (fLogSize < 10) fLogSize = 10;
  950.         fHistSize = dInfo1.histSize;
  951.         fMTPDirID = dInfo1.mtpDirID;
  952.         fLoc = dInfo1.wLoc;
  953.         fSize = dInfo1.wSize;
  954.         configSize = dInfo1.configSize;
  955.         fUseTelnet = TRUE;
  956.         fParseVT100 = TRUE;
  957.         fCvtTab = TRUE;
  958.         fProtocol = 0;
  959.         fFontSize = 9;
  960.         fTileHeight = (int) (fSize.v - 17);
  961.         fTextSig = kTextSig;
  962.     } else {
  963.         count = sizeof (dInfo2);
  964.         FailOSErr (FSRead (aRefNum, &count, (Ptr) &dInfo2));
  965.         fUseCTB = dInfo2.useCTB;
  966.         fDoConnect = dInfo2.doConnect;
  967.         fSkipCR = dInfo2.skip;
  968.         fEcho = dInfo2.echo;
  969.         fUpdate = dInfo2.update;
  970.         fUseTelnet = dInfo2.useTelnet;
  971.         fParseVT100 = dInfo2.parseVT100;
  972.         fCvtTab = dInfo2.cvtTab;
  973.         fEndLine = dInfo2.endLine;
  974.         fInterrupt = dInfo2.intChar;
  975.         fProtocol = dInfo2.protocol;
  976.         fTabSize = dInfo2.rtab;
  977.         fSTab = dInfo2.stab;
  978.         fFontSize = dInfo2.fontSize;
  979.         fTileHeight = dInfo2.tileHeight;
  980.         fTCPPort = dInfo2.tcpPort;
  981.         fMTPPort = dInfo2.mtpPort;
  982.         fLogSize = dInfo2.logSize;
  983.         fHistSize = dInfo2.histSize;
  984.         fMTPDirID = dInfo2.mtpDirID;
  985.         fTextSig = dInfo2.textSig;
  986.         fLoc = dInfo2.wLoc;
  987.         fSize = dInfo2.wSize;
  988.         configSize = dInfo2.configSize;
  989.     }
  990.     if ((theEvent.modifiers & optionKey) != 0) fDoConnect = FALSE;
  991.     fDoEcho = fEcho;
  992.     if (fUseCTB && !gCTBAvailable) {
  993.         fUseCTB = FALSE;
  994.         fDoConnect = FALSE;
  995.         StdAlert (phUnavailID);
  996.         ++fChangeCount;
  997.     } else if (!fUseCTB && !gTCPAvailable) {
  998.         fUseCTB = TRUE;
  999.         fDoConnect = FALSE;
  1000.         StdAlert (phUnavailID);
  1001.         ++fChangeCount;
  1002.     }
  1003.     count = 1;
  1004.     FailOSErr (FSRead (aRefNum, &count, (Ptr) &tmp));
  1005.     count = tmp [0];
  1006.     FailOSErr (FSRead (aRefNum, &count, (Ptr) &(tmp [1])));
  1007.     CopyStr255 (tmp, fMTPVol);
  1008.     count = 1;
  1009.     FailOSErr (FSRead (aRefNum, &count, (Ptr) &tmp));
  1010.     count = tmp [0];
  1011.     FailOSErr (FSRead (aRefNum, &count, (Ptr) &(tmp [1])));
  1012.     CopyStr255 (tmp, fPlayer);
  1013.     count = 1;
  1014.     FailOSErr (FSRead (aRefNum, &count, (Ptr) &tmp));
  1015.     count = tmp [0];
  1016.     FailOSErr (FSRead (aRefNum, &count, (Ptr) &(tmp [1])));
  1017.     CopyStr255 (tmp, fPasswd);
  1018.     count = sizeof (long);
  1019.     FailOSErr (FSRead (aRefNum, &count, (Ptr) &len));
  1020.     SetPermHandleSize ((Handle) fUpdateList, len);
  1021.     oldState = HGetState ((Handle) fUpdateList);
  1022.     HLock ((Handle) fUpdateList);
  1023.     count = len;
  1024.     FailOSErr (FSRead (aRefNum, &count, (Ptr) *fUpdateList));
  1025.     HSetState ((Handle) fUpdateList, oldState);
  1026.     if (fConfig) DisposIfPtr (fConfig);
  1027.     if (configSize != 0) {
  1028.         fConfig = NewPermPtr (configSize);
  1029.         count = configSize;
  1030.         FailOSErr (FSRead (aRefNum, &count, fConfig));
  1031.     }
  1032.     MTPDirCheck (this);
  1033.     if (head.version == 1) {
  1034.         fFontNum = monaco;
  1035.         fontSize = fFontSize;
  1036.         GetPortFontInfo (fFontNum, tmp, &fontSize);
  1037.         CopyStr255 (tmp, fFontName);
  1038.         FailOSErr (OpenResolver (NULL));
  1039.         CatchFailures (&fi, DoReadHandler, kDummyLink);
  1040.         FailOSErr (AddrToStr (fTCPAddr, (char *) tmp));
  1041.         Success (&fi);
  1042.         FailOSErr (CloseResolver ());
  1043.         C2PStr ((Ptr) tmp);
  1044.         CopyStr255 (tmp, fHostName);
  1045.         CopyStr255 ((StringPtr) kCreatorApp, fCreatorApp);
  1046.     } else {
  1047.         count = 1;
  1048.         FailOSErr (FSRead (aRefNum, &count, (Ptr) &tmp));
  1049.         count = tmp [0];
  1050.         FailOSErr (FSRead (aRefNum, &count, (Ptr) &(tmp [1])));
  1051.         CopyStr255 (tmp, fFontName);
  1052.         fFontNum = GetFontNum (tmp);
  1053.         count = 1;
  1054.         FailOSErr (FSRead (aRefNum, &count, (Ptr) &tmp));
  1055.         count = tmp [0];
  1056.         FailOSErr (FSRead (aRefNum, &count, (Ptr) &(tmp [1])));
  1057.         CopyStr255 (tmp, fHostName);
  1058.         count = 1;
  1059.         FailOSErr (FSRead (aRefNum, &count, (Ptr) &tmp));
  1060.         count = tmp [0];
  1061.         FailOSErr (FSRead (aRefNum, &count, (Ptr) &(tmp [1])));
  1062.         CopyStr255 (tmp, fCreatorApp);
  1063.     }
  1064.     if (head.version < 3) {
  1065.         fMaxCmdSize = kMaxCmdSize;
  1066.         fAutoScroll = TRUE;
  1067.         fLogin = NewMacroRec ();
  1068.         fLogout = NewMacroRec ();
  1069.     } else {
  1070.         count = sizeof (dInfo3);
  1071.         FailOSErr (FSRead (aRefNum, &count, (Ptr) &dInfo3));
  1072.         fMaxCmdSize = dInfo3.maxCmdSize;
  1073.         fAutoScroll = dInfo3.autoScroll;
  1074.         fLogin = ReadMacroRec (aRefNum);
  1075.         fLogout = ReadMacroRec (aRefNum);
  1076.         if (dInfo3.numMacros > 0) fMacroList = ReadMacroRec (aRefNum);
  1077.         mh = fMacroList;
  1078.         for (i = 1; i < dInfo3.numMacros; i++) {
  1079.             (**mh).mNext = ReadMacroRec (aRefNum);
  1080.             mh = (**mh).mNext;
  1081.         }
  1082.     }
  1083. }
  1084.  
  1085. //------------------------------------------------------------------------------
  1086.  
  1087. #pragma segment SMUDDocRes
  1088.  
  1089. pascal void TMUDDoc::DoSetupMenus (void)
  1090. {
  1091.     Boolean okTrans;
  1092.     
  1093.     if (gDisableMenus)
  1094.         TEvtHandler::DoSetupMenus ();
  1095.     else {
  1096.         inherited::DoSetupMenus ();
  1097.         CheckConnection ();
  1098.         Enable (cConfigure, TRUE);
  1099.         SetMenuState (cConfigure, kMUDStrings, kmsToolSetup, kmsTCPSetup,
  1100.             fUseCTB);
  1101.         EnableCheck (cUseCTB, gCTBAvailable && gTCPAvailable, fUseCTB);
  1102.         Enable (cPrefs, TRUE);
  1103.         Enable (cComm, TRUE);
  1104.         Enable (cSetup, TRUE);
  1105.         Enable (cMacros, !fRunning);
  1106.         Enable (cLogFile, TRUE);
  1107.         SetMenuState (cLogFile, kMUDStrings, kmsOpenLog, kmsCloseLog,
  1108.             fLogFilter->fLogging);
  1109.         Enable (cConnect, (fBuffer != NULL) && !fRunning && !fClosing);
  1110.         SetMenuState (cConnect, kMUDStrings, kmsConnect, kmsDisconnect,
  1111.             fConnected);
  1112.         okTrans = fConnected && !fRunning && !IsHandlePurged (pMemReserve);
  1113.         if (okTrans && (fProtocol == 0)) okTrans = !fUseCTB && (fMTPPort != 0)
  1114.             && (fPlayer [0] != 0) && (fPasswd [0] != 0);
  1115.         Enable (cUpdate, okTrans);
  1116.         Enable (cUpdateAll, okTrans);
  1117.         Enable (cUpload, okTrans);
  1118.         Enable (cDownload, okTrans);
  1119.         Enable (cBreak, fUseCTB && fConnected && !fRunning);
  1120.         Enable (cInterrupt, fConnected && !fRunning);
  1121.         Enable (cSendFile, fConnected && !fRunning);
  1122.     }
  1123. }
  1124.  
  1125. //------------------------------------------------------------------------------
  1126.  
  1127. #pragma segment SFileTrans
  1128.  
  1129. pascal void TMUDDoc::Download (void)
  1130. {
  1131.     Str255 path, fileName;
  1132.     short vRefNum;
  1133.     TWindow *aWindow;
  1134.     Boolean okPressed;
  1135.     TDialogView *aDialogView;
  1136.     TEditText *aEditText;
  1137.     
  1138.     if (fConnected) {
  1139.         aWindow = NewTemplateWindow (kDownloadID, NULL);
  1140.         FailNIL (aWindow);
  1141.         aEditText = (TEditText *) aWindow->FindSubView ('path');
  1142.         CopyStr255 (fLastPath, path);
  1143.         aEditText->SetText (path, kDontRedraw);
  1144.         aDialogView = (TDialogView *) aWindow->FindSubView ('DLOG');
  1145.         okPressed = aDialogView->PoseModally () == 'OK  ';
  1146.         if (okPressed) aEditText->GetText (path);
  1147.         aWindow->Close ();
  1148.         if (!okPressed) Failure (noErr, msgCancelled);
  1149.         CopyStr255 (path, fLastPath);
  1150.         if (path [0] == 0) Failure (noErr, msgCancelled);
  1151.         RequestName (path, fileName, &vRefNum);
  1152.         if (fProtocol == 0)
  1153.             MTPReceive (this, path, fileName, vRefNum);
  1154.         else
  1155.             EDReceive (this, path, fileName, vRefNum);
  1156.     }
  1157. }
  1158.  
  1159. //------------------------------------------------------------------------------
  1160.  
  1161. #pragma segment MAWriteFile
  1162.  
  1163. pascal void TMUDDoc::DoWrite (short aRefNum, Boolean makingCopy)
  1164. {
  1165.     long count, len;
  1166.     Str255 tmp;
  1167.     DocInfo2 dInfo;
  1168.     DocInfo3 dInfo3;
  1169.     TSizerView *aSizerView;
  1170.     SignedByte oldState;
  1171.     VRect sizerRect;
  1172.     MHandle mh;
  1173.     short i;
  1174.     
  1175.     fLoc = fLogWindow->fLocation;
  1176.     fSize = fLogWindow->fSize;
  1177.     aSizerView = (TSizerView *) fLogWindow->FindSubView ('hors');
  1178.     sizerRect = aSizerView->GetSizerRect (1);
  1179.     fTileHeight = (int) sizerRect.top;
  1180.     count = sizeof (currentHeader);
  1181.     FailOSErr (FSWrite (aRefNum, &count, (Ptr) ¤tHeader));
  1182.     inherited::DoWrite (aRefNum, makingCopy);
  1183.     CopyStr255 (fToolName, tmp);
  1184.     count = tmp [0] + 1;
  1185.     FailOSErr (FSWrite (aRefNum, &count, (Ptr) &tmp));
  1186.     dInfo.useCTB = fUseCTB;
  1187.     dInfo.doConnect = fDoConnect;
  1188.     dInfo.skip = fSkipCR;
  1189.     dInfo.echo = fEcho;
  1190.     dInfo.update = fUpdate;
  1191.     dInfo.useTelnet = fUseTelnet;
  1192.     dInfo.parseVT100 = fParseVT100;
  1193.     dInfo.cvtTab = fCvtTab;
  1194.     dInfo.endLine = (unsigned char) fEndLine;
  1195.     dInfo.intChar = fInterrupt;
  1196.     dInfo.protocol = fProtocol;
  1197.     dInfo.filler1 = 0;
  1198.     dInfo.rtab = fTabSize;
  1199.     dInfo.stab = fSTab;
  1200.     dInfo.fontSize = fFontSize;
  1201.     aSizerView = (TSizerView *) fLogWindow->FindSubView ('hors');
  1202.     sizerRect = aSizerView->GetSizerRect (1);
  1203.     dInfo.tileHeight = (int) sizerRect.top;
  1204.     dInfo.tcpPort = fTCPPort;
  1205.     dInfo.mtpPort = fMTPPort;
  1206.     dInfo.logSize = (short) fLogSize;
  1207.     dInfo.histSize = (short) fHistSize;
  1208.     dInfo.mtpDirID = fMTPDirID;
  1209.     dInfo.textSig = fTextSig;
  1210.     dInfo.wLoc = fLoc;
  1211.     dInfo.wSize = fSize;
  1212.     if (fConfig) {
  1213.         DisposIfPtr (fConfig);
  1214.         fConfig = NULL;
  1215.     }
  1216.     if (fUseCTB) fConfig = CMGetConfig (fConn);
  1217.     if (!fUseCTB || !fConfig)
  1218.         dInfo.configSize = 0;
  1219.     else
  1220.         dInfo.configSize = GetPtrSize (fConfig);
  1221.     count = sizeof (dInfo);
  1222.     FailOSErr (FSWrite (aRefNum, &count, (Ptr) &dInfo));
  1223.     CopyStr255 ((StringPtr) fMTPVol, tmp);
  1224.     count = tmp [0] + 1;
  1225.     FailOSErr (FSWrite (aRefNum, &count, (Ptr) &tmp));
  1226.     CopyStr255 ((StringPtr) fPlayer, tmp);
  1227.     count = tmp [0] + 1;
  1228.     FailOSErr (FSWrite (aRefNum, &count, (Ptr) &tmp));
  1229.     CopyStr255 ((StringPtr) fPasswd, tmp);
  1230.     count = tmp [0] + 1;
  1231.     FailOSErr (FSWrite (aRefNum, &count, (Ptr) &tmp));
  1232.     len = GetHandleSize ((Handle) fUpdateList);
  1233.     count = sizeof (long);
  1234.     FailOSErr (FSWrite (aRefNum, &count, (Ptr) &len));
  1235.     oldState = HGetState ((Handle) fUpdateList);
  1236.     HLock ((Handle) fUpdateList);
  1237.     count = len;
  1238.     FailOSErr (FSWrite (aRefNum, &count, (Ptr) *fUpdateList));
  1239.     HSetState ((Handle) fUpdateList, oldState);
  1240.     if (fConfig) {
  1241.         count = dInfo.configSize;
  1242.         FailOSErr (FSWrite (aRefNum, &count, fConfig));
  1243.         DisposIfPtr (fConfig);
  1244.         fConfig = NULL;
  1245.     }
  1246.     CopyStr255 ((StringPtr) fFontName, tmp);
  1247.     count = tmp [0] + 1;
  1248.     FailOSErr (FSWrite (aRefNum, &count, (Ptr) &tmp));
  1249.     CopyStr255 ((StringPtr) fHostName, tmp);
  1250.     count = tmp [0] + 1;
  1251.     FailOSErr (FSWrite (aRefNum, &count, (Ptr) &tmp));
  1252.     CopyStr255 ((StringPtr) fCreatorApp, tmp);
  1253.     count = tmp [0] + 1;
  1254.     FailOSErr (FSWrite (aRefNum, &count, (Ptr) &tmp));
  1255.     dInfo3.autoScroll = fAutoScroll;
  1256.     dInfo3.filler1 = FALSE;
  1257.     dInfo3.maxCmdSize = fMaxCmdSize;
  1258.     mh = fMacroList;
  1259.     i = 0;
  1260.     while (mh) {
  1261.         i += 1;
  1262.         mh = (**mh).mNext;
  1263.     }
  1264.     dInfo3.numMacros = i;
  1265.     count = sizeof (dInfo3);
  1266.     FailOSErr (FSWrite (aRefNum, &count, (Ptr) &dInfo3));
  1267.     WriteMacroRec (aRefNum, fLogin);
  1268.     WriteMacroRec (aRefNum, fLogout);
  1269.     mh = fMacroList;
  1270.     while (mh) {
  1271.         WriteMacroRec (aRefNum, mh);
  1272.         mh = (**mh).mNext;
  1273.     }
  1274. }
  1275.  
  1276. //------------------------------------------------------------------------------
  1277.  
  1278. #pragma segment MAClose
  1279.  
  1280. pascal void TMUDDoc::Free (void)
  1281. {
  1282.     TFilter *aFilter;
  1283.     MHandle mh, mold;
  1284.     
  1285.     RemHandle ((Handle) this, gMyDocList);
  1286.     CloseConnection ();
  1287.     if (fUpdateList) DisposIfHandle (fUpdateList);
  1288.     if (fBlock) DisposIfPtr (fBlock);
  1289.     if (fConfig) DisposIfPtr (fConfig);
  1290.     while (fInputFilter) {
  1291.         aFilter = fInputFilter->fNext;
  1292.         fInputFilter->Free ();
  1293.         fInputFilter = aFilter;
  1294.     }
  1295.     if (fEchoFilter) fEchoFilter->Free ();
  1296.     if (fLogFilter) fLogFilter->Free ();
  1297.     while (fRunList) fRunList->Free ();
  1298.     DisposMacroRec (fLogin);
  1299.     DisposMacroRec (fLogout);
  1300.     mh = fMacroList;
  1301.     while (mh) {
  1302.         mold = mh;
  1303.         mh = (**mh).mNext;
  1304.         DisposMacroRec (mold);
  1305.     }
  1306.     inherited::Free ();
  1307. }
  1308.  
  1309. //------------------------------------------------------------------------------
  1310.  
  1311. #pragma segment AOpen
  1312.  
  1313. pascal void TMUDDoc::IMUDDoc (void)
  1314. {
  1315.     fConn = NULL;
  1316.     fConfig = NULL;
  1317.     fBuffer = NULL;
  1318.     fIdler = NULL;
  1319.     fBlock = NULL;
  1320.     fUpdateList = NULL;
  1321.     fLogWindow = NULL;
  1322.     fInputFilter = NULL;
  1323.     fEchoFilter = NULL;
  1324.     fLogFilter = NULL;
  1325.     fRunList = NULL;
  1326.     fLogin = NULL;
  1327.     fLogout = NULL;
  1328.     fMacroList = NULL;
  1329.     IDocument (kFileType, kSignature, kUsesDataFork, ! kUsesRsrcFork,
  1330.         ! kDataOpen, ! kRsrcOpen);
  1331.     fBlock = (IOBlock *) NewPermPtr (sizeof (IOBlock));
  1332.     FailNIL (fBlock);
  1333.     fUpdateList = (unsigned char **) NewPermHandle (0);
  1334.     FailNIL (fUpdateList);
  1335.     fBlock->rdBufIn = 0;
  1336.     fBlock->rdBufOut = 0;
  1337.     fBlock->rdBufChars = 0;
  1338.     fBlock->wtBufIn = 0;
  1339.     fBlock->wtBufOut = 0;
  1340.     fBlock->wtBufChars = 0;
  1341.     fBlock->wtCMChars = 0;
  1342.     fConnected = FALSE;
  1343.     fRunning = FALSE;
  1344.     fClosing = FALSE;
  1345.     fTCPAddr = 0;
  1346.     fCurCol = 0;
  1347.     fLastPath [0] = 0;
  1348.     AddHandle ((Handle) this, gMyDocList);
  1349.     fInputFilter = new TTelnetFilter;
  1350.     FailNIL (fInputFilter);
  1351.     fInputFilter->IFilter (this);
  1352.     fInputFilter->fNext = new TVT100Filter;
  1353.     FailNIL (fInputFilter->fNext);
  1354.     fInputFilter->fNext->IFilter (this);
  1355.     fMacroFilter = new TMacroFilter;
  1356.     FailNIL (fMacroFilter);
  1357.     fInputFilter->fNext->fNext = fMacroFilter;
  1358.     fMacroFilter->IFilter (this);
  1359.     fLogFilter = new TLogFilter;
  1360.     FailNIL (fLogFilter);
  1361.     fLogFilter->IFilter (this);
  1362.     fEchoFilter = new TEchoFilter;
  1363.     FailNIL (fEchoFilter);
  1364.     fEchoFilter->IFilter (this);
  1365.     fLastMacItem = 1;
  1366.     fQuiet = FALSE;
  1367. }
  1368.  
  1369. //------------------------------------------------------------------------------
  1370.  
  1371. #pragma segment SMUDDocRes
  1372.  
  1373. pascal void TMUDDoc::Interrupt (void)
  1374. {
  1375.     unsigned char ch;
  1376.     
  1377.     if (fConnected) {
  1378.         ch = fInterrupt;
  1379.         Send (&ch, 1);
  1380.     }
  1381. }
  1382.  
  1383. //------------------------------------------------------------------------------
  1384.  
  1385. #pragma segment AOpen
  1386.  
  1387. pascal void TMUDDoc::OpenConnection (void)
  1388. {
  1389.     TCPiopb pb;
  1390.     Str255 tmp;
  1391.     int procID;
  1392.     CMBufferSizes sizes = {kCMBufferSize, kCMBufferSize};
  1393.     Boolean toolFound, wasPermanent;
  1394.  
  1395.     if (fUseCTB) {
  1396.         CopyStr255 (fToolName, tmp);
  1397.         procID = CMGetProcID (tmp);
  1398.         if (procID == -1) {
  1399.             toolFound = (tmp [0] == 0);
  1400.             fDoConnect = FALSE;
  1401.             FailOSErr (CRMGetIndToolName (classCM, 1, tmp));
  1402.             procID = CMGetProcID (tmp);
  1403.             CopyStr255 (tmp, fToolName);
  1404.         } else
  1405.             toolFound = TRUE;
  1406.         if (procID == -1) Failure (errNoTools, msgOpenFailed);
  1407.         wasPermanent = PermAllocation (TRUE);
  1408.         fConn = CMNew (procID, cmData, sizes, 0, 0);
  1409.         wasPermanent = PermAllocation (wasPermanent);
  1410.         FailNIL (fConn);
  1411.         MoveHHi ((Handle) fConn);
  1412.         HLock ((Handle) fConn);
  1413.         if (fBuffer) DisposIfPtr (fBuffer);
  1414.         fBuffer = NewPermPtr (sizes [cmDataIn]);
  1415.         FailNIL (fBuffer);
  1416.         if (fConfig != NULL) {
  1417.             if (toolFound) CMSetConfig (fConn, fConfig);
  1418.             DisposIfPtr (fConfig);
  1419.             fConfig = NULL;
  1420.         }
  1421.         CMActivate (fConn, TRUE);
  1422.         if (!toolFound) {
  1423.             StdAlert (phUnavailID);
  1424.             ++fChangeCount;
  1425.         }
  1426.     } else {
  1427.         if (fBuffer) DisposIfPtr (fBuffer);
  1428.         fBuffer = NewPermPtr (kTCPBufferSize);
  1429.         FailNIL (fBuffer);
  1430.         pb.ioNamePtr = NULL;
  1431.         pb.ioCRefNum = gTCPRef;
  1432.         pb.csCode = TCPCreate;
  1433.         pb.csParam.create.rcvBuff = fBuffer;
  1434.         pb.csParam.create.rcvBuffLen = kTCPBufferSize;
  1435.         pb.csParam.create.notifyProc = ASRDummy;
  1436.         pb.csParam.create.userDataPtr = (Ptr) fBlock;
  1437.         FailOSErr (PBControl ((ParamBlockRec *) &pb, FALSE));
  1438.         fTCPStream = pb.tcpStream;
  1439.     }
  1440.     if (fDoConnect) Connect ();
  1441.     fIdler = new TMUDIdler;
  1442.     FailNIL (fIdler);
  1443.     ((TMUDIdler *) fIdler)->IMUDIdler (this);
  1444.     gApplication->InstallCohandler (fIdler, TRUE);
  1445. }
  1446.  
  1447. //------------------------------------------------------------------------------
  1448.  
  1449. #pragma segment SFileTrans
  1450.  
  1451. pascal void TMUDDoc::RequestName (Str255 path, Str255 name, short *vRefNum)
  1452. {
  1453.     SFReply reply;
  1454.     short dlgID, pos;
  1455.     Str255 tmp;
  1456.     Point dlgLoc;
  1457.     DlgHookProcPtr dlgHook;
  1458.     ModalFilterProcPtr filterProc;
  1459.     OSErr err;
  1460.     
  1461.     pos = path [0];
  1462.     for (pos = path [0]; (pos > 0) && (path [pos] != '/'); pos--);
  1463.     name [0] = path [0] - pos;
  1464.     for (int i = 1; i <= name [0]; i++) name [i] = path [pos + i];
  1465.     SFPutParms (cDownload, &dlgID, &dlgLoc, name, tmp, (Ptr *) &dlgHook,
  1466.         (Ptr *) &filterProc);
  1467.     gApplication->UpdateAllWindows ();
  1468.     SFPPutFile (dlgLoc, tmp, name, dlgHook, &reply, dlgID, filterProc);
  1469.     if (reply.good) {
  1470.         CopyStr255 (reply.fName, name);
  1471.         *vRefNum = reply.vRefNum;
  1472.         err = DeleteFile (name, *vRefNum);
  1473.         if (err != noErr && err != fnfErr) Failure (err, 0);
  1474.     } else
  1475.         Failure (noErr, msgCancelled);
  1476. }
  1477.  
  1478. //------------------------------------------------------------------------------
  1479.  
  1480. #pragma segment SMUDDocRes
  1481.  
  1482. pascal void TMUDDoc::Send (unsigned char *theChars, long count)
  1483. {
  1484.     long act;
  1485.     
  1486.     if (fConnected && fDoEcho && !fQuiet) {
  1487.         act = 0;
  1488.         do {
  1489.             act += fEchoFilter->Filter (theChars + act, count - act);
  1490.         } while (act < count);
  1491.     }
  1492.     SendNoEcho (theChars, count);
  1493. }
  1494.  
  1495. //------------------------------------------------------------------------------
  1496.  
  1497. #pragma segment SMUDDocRes
  1498.  
  1499. static short dRef, rRef;
  1500.  
  1501. pascal void SendFileHandler (short , long , void * )
  1502. {
  1503.     CloseFile (dRef, rRef);
  1504. }
  1505.  
  1506. pascal void TMUDDoc::SendFile (void)
  1507. {
  1508.     AppFile aFile;
  1509.     FailInfo fi;
  1510.     unsigned char buf[kTmpBufSize + 1];
  1511.     long count;
  1512.     OSErr err;
  1513.     
  1514.     if (fConnected && gApplication->ChooseDocument (cSendFile, &aFile)) {
  1515.         FailOSErr (MAOpenFile (aFile.fName, aFile.vRefNum, TRUE, FALSE,
  1516.             fsRdPerm, fsRdPerm, &dRef, &rRef));
  1517.         CatchFailures (&fi, SendFileHandler, kDummyLink);
  1518.         do {
  1519.             count = kTmpBufSize;
  1520.             err = FSRead (dRef, &count, (Ptr) &buf);
  1521.             if ((err != noErr) && (err != eofErr)) FailOSErr (err);
  1522.             Send ((unsigned char *) &buf, count);
  1523.         } while (count == kTmpBufSize);
  1524.         Success (&fi);
  1525.         FailOSErr (CloseFile (dRef, rRef));
  1526.         FailOSErr (FlushVol (NULL, aFile.vRefNum));
  1527.     }
  1528. }
  1529.  
  1530. //------------------------------------------------------------------------------
  1531.  
  1532. #pragma segment SMUDDocRes
  1533.  
  1534. static Boolean wasDisabled;
  1535.  
  1536. pascal void SendNoEchoHandler (short , long , void * )
  1537. {
  1538.     gDisableMenus = FALSE;
  1539. }
  1540.  
  1541. pascal void TMUDDoc::SendNoEcho (unsigned char *theChars, long count)
  1542. {
  1543.     long act;
  1544.     unsigned char ch;
  1545.     FailInfo fi;
  1546.     
  1547.     wasDisabled = gDisableMenus;
  1548.     act = 0;
  1549.     while (count > act) {
  1550.         while ((count > act) && (fBlock->wtBufChars < kRWBufferSize - 1)) {
  1551.             ch = *theChars++;
  1552.             if (ch == chReturn) {
  1553.                 switch (fEndLine) {
  1554.                 case 0:
  1555.                     fBlock->wtBuf [fBlock->wtBufIn] = chReturn;
  1556.                     break;
  1557.                 case 1:
  1558.                     fBlock->wtBuf [fBlock->wtBufIn] = chReturn;
  1559.                     ++(fBlock->wtBufIn);
  1560.                     if (fBlock->wtBufIn >= kRWBufferSize) fBlock->wtBufIn = 0;
  1561.                     ++(fBlock->wtBufChars);
  1562.                     fBlock->wtBuf [fBlock->wtBufIn] = chLineFeed;
  1563.                     break;
  1564.                 case 2:
  1565.                     fBlock->wtBuf [fBlock->wtBufIn] = chLineFeed;
  1566.                     break;
  1567.                 }
  1568.                 fCurCol = 0;
  1569.             } else {
  1570.                 fBlock->wtBuf [fBlock->wtBufIn] = ch;
  1571.                 ++fCurCol;
  1572.             }
  1573.             ++(fBlock->wtBufIn);
  1574.             if (fBlock->wtBufIn >= kRWBufferSize) fBlock->wtBufIn = 0;
  1575.             ++(fBlock->wtBufChars);
  1576.             ++act;
  1577.         }
  1578.         if (count > act) {
  1579.             if (!gDisableMenus) {
  1580.                 CatchFailures (&fi, SendNoEchoHandler, kDummyLink);
  1581.                 gDisableMenus = TRUE;
  1582.                 InvalidateMenuBar ();
  1583.             }
  1584.             gApplication->PollEvent (kAllowApplicationToSleep);
  1585.             if (gStop) Failure (noErr, msgCancelled);
  1586.         }
  1587.     }
  1588.     if (gDisableMenus && !wasDisabled) {
  1589.         Success (&fi);
  1590.         gDisableMenus = FALSE;
  1591.         InvalidateMenuBar ();
  1592.     }
  1593. }
  1594.  
  1595. //------------------------------------------------------------------------------
  1596.  
  1597. #pragma segment SFileTrans
  1598.  
  1599. static TWindow *anUpdater;
  1600.  
  1601. pascal void UpdateHandler (short , long , void * )
  1602. {
  1603.     anUpdater->Close();
  1604. }
  1605.  
  1606. pascal void TMUDDoc::Update (void)
  1607. {
  1608.     Str255 path;
  1609.     FailInfo fi;
  1610.     
  1611.     anUpdater = NewTemplateWindow (kUpdateID, NULL);
  1612.     FailNIL (anUpdater);
  1613.     CatchFailures (&fi, UpdateHandler, kDummyLink);
  1614.     gApplication->CommitLastCommand ();
  1615.     anUpdater->Open();
  1616.     anUpdater->Select();
  1617.     do {
  1618.         gApplication->PollEvent (kAllowApplicationToSleep);
  1619.         if (gStop) Failure (noErr, msgCancelled);
  1620.     } while (gIdlePhase != idleContinue);
  1621.     path [0] = 0;
  1622.     UpdateFrom (anUpdater, fMTPDirID, path);
  1623.     Success (&fi);
  1624.     anUpdater->Close();
  1625. }
  1626.  
  1627. //------------------------------------------------------------------------------
  1628.  
  1629. #pragma segment SFileTrans
  1630.  
  1631. pascal void TMUDDoc::UpdateAll (void)
  1632. {
  1633.     Str255 path;
  1634.     FailInfo fi;
  1635.     
  1636.     anUpdater = NewTemplateWindow (kUpdateID, NULL);
  1637.     FailNIL (anUpdater);
  1638.     CatchFailures (&fi, UpdateHandler, kDummyLink);
  1639.     gApplication->CommitLastCommand ();
  1640.     anUpdater->Open();
  1641.     anUpdater->Select();
  1642.     do {
  1643.         gApplication->PollEvent (kAllowApplicationToSleep);
  1644.         if (gStop) Failure (noErr, msgCancelled);
  1645.     } while (gIdlePhase != idleContinue);
  1646.     SetPermHandleSize ((Handle) fUpdateList, 0);
  1647.     path [0] = 0;
  1648.     UpdateFrom (anUpdater, fMTPDirID, path);
  1649.     Success (&fi);
  1650.     anUpdater->Close();
  1651.     ++fChangeCount;
  1652. }
  1653.  
  1654. //------------------------------------------------------------------------------
  1655.  
  1656. #pragma segment SFileTrans
  1657.  
  1658. static short wdRef;
  1659.  
  1660. pascal void UpdateFileHandler (short , long , void * )
  1661. {
  1662.     WDPBRec pb;
  1663.  
  1664.     pb.ioNamePtr = NULL;
  1665.     pb.ioVRefNum = wdRef;
  1666.     PBCloseWD (&pb, FALSE);
  1667. }
  1668.  
  1669. pascal void TMUDDoc::UpdateFile (TWindow *anUpdater, long dirID, Str255 fName,
  1670.         unsigned long mDate, Str255 path)
  1671. {
  1672.     long listLen, offset, i, len, date;
  1673.     unsigned char *name, *tmp;
  1674.     AppFile theFile;
  1675.     WDPBRec pb;
  1676.     FailInfo fi;
  1677.     Str255 aStr;
  1678.     TStaticText *aStaticText;
  1679.  
  1680.     offset = 0;
  1681.     listLen = GetHandleSize ((Handle) fUpdateList);
  1682.     while (offset < listLen) {
  1683.         name = (unsigned char *) *fUpdateList + offset + sizeof (long);
  1684.         len = *name++;
  1685.         if (len == path [0]) {
  1686.             tmp = path + 1;
  1687.             for (i = len; i > 0; --i)
  1688.                 if (*name++ != *tmp++) break;
  1689.             if (i == 0) break;
  1690.         }
  1691.         offset += len + 1 + sizeof (long);
  1692.     }
  1693.     if (offset >= listLen) {
  1694.         SetPermHandleSize ((Handle) fUpdateList, offset + sizeof (long) +
  1695.             path [0] + 1);
  1696.         date = 0;
  1697.         name = (unsigned char *) *fUpdateList + offset + sizeof (long);
  1698.         tmp = path;
  1699.         for (i = path [0]; i >= 0; --i) *name++ = *tmp++;
  1700.     } else {
  1701.         tmp = (unsigned char *) *fUpdateList + offset;
  1702.         date = *tmp++ << 24;
  1703.         date += *tmp++ << 16;
  1704.         date += *tmp++ << 8;
  1705.         date += *tmp;
  1706.     }
  1707.     if (date < mDate) {
  1708.         tmp = (unsigned char *) *fUpdateList + offset;
  1709.         *tmp++ = 0;
  1710.         *tmp++ = 0;
  1711.         *tmp++ = 0;
  1712.         *tmp = 0;
  1713.         aStaticText = (TStaticText *) ((TMUDUpdater *) anUpdater)->
  1714.             FindSubView ('name');
  1715.         aStaticText->SetText (path, kRedraw);
  1716.         pb.ioNamePtr = NULL;
  1717.         pb.ioVRefNum = fMTPVRefNum;
  1718.         pb.ioWDProcID = kSignature;
  1719.         pb.ioWDDirID = dirID;
  1720.         FailOSErr (PBOpenWD (&pb, FALSE));
  1721.         wdRef = pb.ioVRefNum;
  1722.         CatchFailures (&fi, UpdateFileHandler, kDummyLink);
  1723.         do {
  1724.             gApplication->PollEvent (kAllowApplicationToSleep);
  1725.             if (gStop) Failure (noErr, msgCancelled);
  1726.         } while (gIdlePhase != idleContinue);
  1727.         theFile.vRefNum = wdRef;
  1728.         CopyStr255 (fName, theFile.fName);
  1729.         if (fProtocol == 0)
  1730.             MTPSend (this, &theFile, path);
  1731.         else
  1732.             EDSend (this, &theFile, path);
  1733.         Success (&fi);
  1734.         pb.ioNamePtr = NULL;
  1735.         pb.ioVRefNum = wdRef;
  1736.         FailOSErr (PBCloseWD (&pb, FALSE));
  1737.         tmp = (unsigned char *) *fUpdateList + offset;
  1738.         *tmp++ = (unsigned char) ((mDate >> 24) & 0xFF);
  1739.         *tmp++ = (unsigned char) ((mDate >> 16) & 0xFF);
  1740.         *tmp++ = (unsigned char) ((mDate >> 8) & 0xFF);
  1741.         *tmp = (unsigned char) (mDate & 0xFF);
  1742.         if (fUpdate) {
  1743.             len = path [0];
  1744.             if ((len >= 2) && (path [len - 1] == '.') && (path [len] == 'c')) {
  1745.                 path [0] = (unsigned char) (len - 2);
  1746.                 GetIndString (aStr, kMUDStrings, kmsUpdate);
  1747.                 Send (aStr + 1, aStr [0]);
  1748.                 Send (path + 1, path [0]);
  1749.                 gPrompt = FALSE;
  1750.                 aStr [0] = chReturn;
  1751.                 Send (aStr, 1);
  1752.                 while (!gPrompt) {
  1753.                     gApplication->PollEvent (kAllowApplicationToSleep);
  1754.                     if (gStop) Failure (noErr, msgCancelled);
  1755.                 }
  1756.             }
  1757.         }
  1758.         ++fChangeCount;
  1759.     }
  1760. }
  1761.  
  1762. //------------------------------------------------------------------------------
  1763.  
  1764. #pragma segment SFileTrans
  1765.  
  1766. pascal void TMUDDoc::UpdateFrom (TWindow *anUpdater, long dirID, Str255 path)
  1767. {
  1768.     int index, len;
  1769.     OSErr err;
  1770.     CInfoPBRec pb;
  1771.     Str255 fName;
  1772.     
  1773.     len = path [0];
  1774.     pb.hFileInfo.ioNamePtr = fName;
  1775.     pb.hFileInfo.ioVRefNum = fMTPVRefNum;
  1776.     index = 1;
  1777.     do {
  1778.         pb.hFileInfo.ioFDirIndex = index;
  1779.         pb.hFileInfo.ioDirID = dirID;
  1780.             /* set to ioFlNum by GetCatInfo */
  1781.         err = PBGetCatInfo (&pb, FALSE);
  1782.         if (err != fnfErr) FailOSErr (err);
  1783.         if (err == noErr) {
  1784.             if (len > 0) PStrCat (path, "\p/");
  1785.             PStrCat (path, fName);
  1786.             if ((pb.hFileInfo.ioFlAttrib & ioDirMask) != 0)
  1787.                 UpdateFrom (anUpdater, pb.hFileInfo.ioDirID, path);
  1788.             else
  1789.                 UpdateFile (anUpdater, dirID, fName, pb.hFileInfo.ioFlMdDat,
  1790.                     path);
  1791.             path [0] = len;
  1792.         }
  1793.         ++index;
  1794.     } while (err == noErr);
  1795. }
  1796.  
  1797. //------------------------------------------------------------------------------
  1798.  
  1799. #pragma segment SFileTrans
  1800.  
  1801. pascal void TMUDDoc::Upload (void)
  1802. {
  1803.     AppFile aFile;
  1804.     Str255 path, aStr;
  1805.     TWindow *aWindow;
  1806.     Boolean okPressed;
  1807.     TDialogView *aDialogView;
  1808.     TEditText *aEditText;
  1809.     int len;
  1810.     
  1811.     if (fConnected && gApplication->ChooseDocument (cUpload, &aFile)) {
  1812.         MTPBuildPath (this, &aFile, path);
  1813.         aWindow = NewTemplateWindow (kUploadID, NULL);
  1814.         FailNIL (aWindow);
  1815.         aEditText = (TEditText *) aWindow->FindSubView ('path');
  1816.         aEditText->SetText (path, kDontRedraw);
  1817.         aDialogView = (TDialogView *) aWindow->FindSubView ('DLOG');
  1818.         okPressed = aDialogView->PoseModally () == 'OK  ';
  1819.         if (okPressed) aEditText->GetText (path);
  1820.         aWindow->Close ();
  1821.         if (!okPressed) Failure (noErr, msgCancelled);
  1822.         if (fProtocol == 0)
  1823.             MTPSend (this, &aFile, path);
  1824.         else
  1825.             EDSend (this, &aFile, path);
  1826.         if (fUpdate) {
  1827.             len = path [0];
  1828.             if ((len >= 2) && (path [len - 1] == '.') && (path [len] == 'c')) {
  1829.                 path [0] = len - 2;
  1830.                 GetIndString (aStr, kMUDStrings, kmsUpdate);
  1831.                 Send (aStr + 1, aStr [0]);
  1832.                 Send (path + 1, path [0]);
  1833.                 aStr [0] = chReturn;
  1834.                 Send (aStr, 1);
  1835.             }
  1836.         }
  1837.     }
  1838. }
  1839.  
  1840. //------------------------------------------------------------------------------
  1841.  
  1842. #pragma segment SMUDDocRes
  1843.  
  1844. pascal void TMUDDoc::UseCTB (void)
  1845. {
  1846.     CloseConnection ();
  1847.     fUseCTB = !fUseCTB;
  1848.     fDoConnect = FALSE;
  1849.     OpenConnection ();
  1850.     ++fChangeCount;
  1851. }
  1852.  
  1853. //------------------------------------------------------------------------------
  1854.  
  1855. #pragma segment SMUDDocRes
  1856. // CMtoOSErr must be in same segment as WtCompletor!!!
  1857.  
  1858. pascal OSErr CMtoOSErr (CMErr err)
  1859. {
  1860.     OSErr oe;
  1861.     
  1862.     oe = noErr;
  1863.     switch (err) {
  1864.     case cmGenericError:
  1865.         oe = errCMGeneric;
  1866.         break;
  1867.     case cmRejected:
  1868.         oe = errCMRejected;
  1869.         break;
  1870.     case cmFailed:
  1871.         oe = errCMFailed;
  1872.         break;
  1873.     case cmTimeOut:
  1874.         oe = errCMTimeOut;
  1875.         break;
  1876.     case cmNotOpen:
  1877.         oe = errCMNotOpen;
  1878.         break;
  1879.     case cmNotClosed:
  1880.         oe = errCMNotClosed;
  1881.         break;
  1882.     case cmNoRequestPending:
  1883.         oe = errCMNoRequest;
  1884.         break;
  1885.     case cmNotSupported:
  1886.         oe = errCMNotSup;
  1887.         break;
  1888.     case cmNoTools:
  1889.         oe = errCMNoTools;
  1890.         break;
  1891.     default:
  1892.         if (err > 0)
  1893.             oe = errCMGeneric;
  1894.         else
  1895.             oe = err;
  1896.         break;
  1897.     }
  1898.     return oe;
  1899. }
  1900.  
  1901. //------------------------------------------------------------------------------
  1902.  
  1903. #pragma segment SMUDDocRes
  1904.  
  1905. pascal void FailCMErr (CMErr err)
  1906. {
  1907.     OSErr oe;
  1908.     
  1909.     oe = CMtoOSErr (err);
  1910.     if (oe != noErr) Failure (oe, 0);
  1911. }
  1912.  
  1913. //------------------------------------------------------------------------------
  1914.  
  1915. #pragma segment MAInit
  1916.  
  1917. pascal void InitMUDDoc (void)
  1918. {
  1919.     if (gDeadStripSuppression) {
  1920.         TMUDUpdater *aMUDUpdater = new TMUDUpdater;
  1921.     }
  1922. }
  1923.  
  1924. //------------------------------------------------------------------------------
  1925.